package com.astrob.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.NavFrameSDK;
import com.astrob.NavFrameSDK.POIDescription;
import com.astrob.nhcmap.R;

public class CitysAdapter extends BaseAdapter {
	static class ViewHolder {
		TextView text;
	}

	protected Context mContext;
	protected List<POIDescription>  mCityList;

	public CitysAdapter(Context context) {
		mContext = context;
	}
	
	public boolean isRevertBk = false;
	
	public void setCityDatas(List<POIDescription> citylist) {
		mCityList = citylist;
	}

	public void setNameDatas(List<String> namelist) {
		if (namelist == null) {
			return;
		}

		mCityList = new ArrayList<POIDescription>();
		for (String name : namelist) {
			POIDescription data = NavFrameSDK.getInstance().new POIDescription();
			data.name = name;
			mCityList.add(data);
		}
	}

	@Override
	public int getCount() {
		if (mCityList != null) {
			return mCityList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if (mCityList != null) {
			return mCityList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			if (isRevertBk) {
				convertView = LinearLayout.inflate(mContext, R.layout.lista2, null);
			} else {
				convertView = LinearLayout.inflate(mContext, R.layout.lista, null);
			}			
			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.listAText);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String name = "";
		if (mCityList != null) {
			name = mCityList.get(position).name;
			String[] names = name.split("\n");
			if (names.length == 2) {
				name = names[1];
			}
		}
		holder.text.setText(name);
		if (selectedPosition == position) {
			holder.text.setSelected(true);
			holder.text.setPressed(true);
			holder.text.setBackgroundColor(Color.WHITE);
		} else {
			holder.text.setSelected(false);
			holder.text.setPressed(false);
			holder.text.setBackgroundColor(Color.TRANSPARENT);
		}

		return convertView;
	}

	private int selectedPosition = -1;// 选中的位置
	
	public void setSelectedPosition(int position) {
		selectedPosition = position;  
	}

}
